<script src="../../models/blog.js"></script>
<script src="../../routers/admin.js"></script>
<script src="../../routers/api.js"></script>{% extends 'layout.html'%}
{% block title%}添加博客{%endblock%}
{% block body%}
{% block scripts%}
<script src="../../public/js/tinymce/tinymce.min.js" referrerpolicy="origin"></script>
<script>
    tinymce.init({
        selector:'textarea',
        language: 'zh_CN',//语言
        height: 300,//编辑器高度
        branding: false,//是否禁用“Powered by TinyMCE”
        plugins: [
            'powerpaste table advlist autolink lists link charmap print preview hr anchor pagebreak',
            'searchreplace wordcount visualblocks visualchars code fullscreen',
            'insertdatetime nonbreaking save table contextmenu directionality',
            'emoticons textcolor colorpicker textpattern image code codesample toc pagebreak'
        ],
        toolbar1: 'undo redo | table | insert | styleselect | bold italic |fontsizeselect| alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image',
        toolbar2: 'print preview | forecolor backcolor emoticons | codesample | pagebreak | toc | fullscreen|code',
        image_advtab: true,
        paste_data_images: true,
        menubar: false,//禁用标题栏
        automatic_uploads: true,
        media_live_embeds: true,//查看上传的视频
        //图片选择上传
        images_upload_handler: function (blobInfo, success, failure) {
            var xhr, formData;
            var file = blobInfo.blob();//转化为易于理解的file对象
            var isLt10M = file.size / 1024 / 1024  < 10;
            if(!isLt10M){
                failure('上传图片大小不能超过10MB哦!');
                return;
            }
            xhr = new XMLHttpRequest();
            xhr.withCredentials = false;
            xhr.open('POST',ctx + 'file/uploadFile?editorImg=editorImg&target=oss');
            xhr.onload = function() {
                var json;
                if (xhr.status != 200) {
                    failure('上传失败!');
                    return;
                }
                json = JSON.parse(xhr.responseText);
                if (!json.data || typeof json.data.filePath != 'string') {
                    failure('上传失败!');
                    return;
                }
                success(json.data.filePath);
            };
            formData = new FormData();
            formData.append('file', file, file.name );//此处与源文档不一样
            xhr.send(formData);
        },

    });
</script>
<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js">
</script>
<script>
    $(function(){
        let $category =$("#category");
        let $title = $("#title");
        let $author = $("#author");
        let $synopsis =$ ("#synopsis");
        let $content =$("#content");
        let $id =$("#blogId");
        $("#submit").click(function(){
            $.post("/admin/blog/add",{
                    blogId :$id.val(),
                    category:$category.val(),
                    title:$title.val(),
                    author:$author.val(),
                    synopsis:$synopsis.val(),
                    content:tinyMCE.activeEditor.getContent()
                },
                function(result){
                if(result.msg=="保存成功"){
                    alert("提交成功");
                }else if (result.msg=="修改成功"){
                    alert("修改成功");
                }
                    window.location.href='/admin/blog?page=1'
                });
        });
    });
</script>
{%endblock%}
<form>
    <input id="blogId" hidden value="{{blog._id.toString()}}">
    <div class="form-group" >
        <h4>分类</small></h4>
        <select id="category" class="form-control" >
            <option>{{blog.category}}</option>
            {% for label in categorys %}
            <option>{{label.name}}</option>
            {% endfor%}
        </select>
    </div>
    <div class="form-group" >
        <h4>标题</small></h4>
        <input id="title" type="text" class="form-control" placeholder="标题" value="{{blog.title}}">
    </div>
    <div class="form-group" >
        <h4>作者</small></h4>
        <input id="author" type="text" class="form-control" placeholder="作者" value="{{blog.author}}">
    </div>
    <div class="form-group" >
        <h4>简介</small></h4>
        <input id="synopsis" class="form-control" rows="3" value="{{blog.synopsis}}">
    </div>
    <div class="form-group" >
        <h4>内容</small></h4>
        <textarea id="content">{{blog.content}}</textarea>
        <!--<textarea id="content" class="form-control" rows="3">{{blog.content}}</textarea>-->
    </div>

</form>

<button id="submit" type="button" class="btn btn-primary btn-lg btn-block">提交</button>
{% endblock%}